﻿using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace movie.tool.library.utils
{
    public class XmlHelper
    {
        public static string ParseInitPartUploadId(string s)
        {
            if (string.IsNullOrEmpty(s))
                throw new ArgumentNullException("s");
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(s);
            XmlNode result = FindChildNode(doc, "InitiateMultipartUploadResult");
            if (result != null)
            {
                XmlNode node = FindChildNode(result, "UploadId");
                if(node != null)
                {
                    return node.InnerText;
                }
            }
            return string.Empty;
        }

        public static XmlNode FindChildNode(XmlNode node, string name) 
        {
            if (node.HasChildNodes)
            {
                foreach (XmlNode n in node.ChildNodes)
                {
                    if (object.Equals(name, n.Name))
                    {
                        return n;
                    }
                }
            }
            return null;
        }

    }
}
